package com.aiwei.tdjk.controller;

import com.aiwei.tdjk.common.Constants;
import com.aiwei.tdjk.dto.request.CustomsHandbookUsedDetailPagerRequest;
import com.aiwei.tdjk.entity.CustomsHandbookUsedDetail;
import com.aiwei.tdjk.service.CustomsHandbookService;
import com.aiwei.tdjk.service.CustomsHandbookUsedDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;
import java.util.List;

@Controller
@RequestMapping(value = "/admin/customsHandbookUsedDetail")
public class CustomsHandbookUsedDetailController extends BaseController {

	@Autowired
	private CustomsHandbookUsedDetailService customsHandbookUsedDetailService;

	@Autowired
	private CustomsHandbookService customsHandbookService;


	@RequestMapping(value = {"/list"}, method = RequestMethod.GET)
	public String list(CustomsHandbookUsedDetailPagerRequest request, Model model) {
	    if (request.getCustomsHandBookId()!=null){
            List<Long> handbookProductdetailsIDs = customsHandbookUsedDetailService.getHandbookProductdetailsIDs(request.getCustomsHandBookId());
            request.setCustomsHandbookProductdetailIds(handbookProductdetailsIDs);
        }
        model.addAttribute("page", customsHandbookUsedDetailService.find(request));
		model.addAttribute("customsHandbookUsedDetail", request);
		model.addAttribute("customsHandbooks", customsHandbookService.getAllAvailable());
		return viewName("list");
	}

    @RequestMapping(value = "create", method = RequestMethod.GET)
    public String showCreateForm(Model model) {
        model.addAttribute(Constants.OP_NAME, "新增");
        if (!model.containsAttribute("m")) {
		CustomsHandbookUsedDetail m = new CustomsHandbookUsedDetail();
        model.addAttribute("m", m);
        }
        return viewName("editForm");
	}

    @RequestMapping(value = "create", method = RequestMethod.POST)
    public String create(
            Model model, @Valid @ModelAttribute("m") CustomsHandbookUsedDetail m, BindingResult result,
            RedirectAttributes redirectAttributes) {
        if (hasError(result)) {
        return showCreateForm(model);
        }
		customsHandbookUsedDetailService.add(m);
        redirectAttributes.addFlashAttribute(Constants.MESSAGE, "新增成功");
        return redirectToUrl("list");
	}

	@RequestMapping(value = "{id}/update", method = RequestMethod.GET)
	public String showUpdateForm(@PathVariable("id") Long id, Model model) {
        model.addAttribute(Constants.OP_NAME, "修改");
        model.addAttribute("m", customsHandbookUsedDetailService.get(id));
        return viewName("editForm");
	}

	@RequestMapping(value = "{id}/update", method = RequestMethod.POST)
	public String update(
        Model model, @Valid @ModelAttribute("m") CustomsHandbookUsedDetail m, BindingResult result,
        RedirectAttributes redirectAttributes) {
        if (hasError(result)) {
        return showUpdateForm(m.getId(), model);
        }
		customsHandbookUsedDetailService.update(m);
        redirectAttributes.addFlashAttribute(Constants.MESSAGE, "修改成功");
        return redirectToUrl("list");
	}

    @RequestMapping(value = "getListByCustomerHandBookId", method = RequestMethod.GET)
    public String getListByCustomerHandBookId(Long customsHandBookId, Model model) {
        model.addAttribute("usedDetailList", customsHandbookUsedDetailService.getListByCustomerHandBookId(customsHandBookId));
        return viewName("usedList");
    }


}
